/**
 * 开始界面
 * 实现开始界面逻辑
 */
class Gamebegin extends moon.BasicGameStart {
    public btn: MButton;//开始按钮
    private buttonr: MButton;//排行榜按钮
    private music: MButton;//音乐监听按钮关闭
    private music_remove: MButton;//音乐监听按钮开关
    private playing: MButton;//玩法按钮
    private close: MButton;//玩法关闭
    private playingimage: MImage;//图片
    private nickName: any;
    protected render(): void {
        super.render();
        /**开始界面背景图 */
        this.createImageBg("beijing_png");

        // /**背景音乐预先加载好 */
        // SoundControl.getIns().play(MUSIC_BG, 0, 9999);//背景音乐

        //   this.btnRestartadd = this.createMButton("kaishi_png", 340, 800);

        /**昵称 */
        this.txtMyScore = this.createText(10, 120);
        this.updateScore();
        /**获取微信数据 */
        this.GetMessage();
        /**资源加载 */
        this.upload();

        // window.href = "http://www.baidu.com";
        // window.open("http://www.baidu.com");
    }
    protected initView(): void {
        //需要复写
    }
    /**获取 */
    protected initGame(): void {
        this.updateScore();//昵称

    }

    /**资源加载 */
    private upload() {
        /**开始按钮 */
        this.btn = new MButton(new MImage("kaishi_png"), new MImage("kaishi_png"));
        this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClick, this);
        this.btn.x = 340; this.btn.y = 800;
        this.addChild(this.btn);
        egret.Tween.get(this.btn).to({ scaleX: 0.8, scaleY: 0.8 }, 100).to({ scaleX: 1, scaleY: 1 }, 100);
        /**音乐关闭 */
        this.music = new MButton(new MImage("shengyin_png"), new MImage("shengyin_png"));
        this.music.x = 560; this.music.y = 10;
        this.music.addEventListener(egret.TouchEvent.TOUCH_TAP, this.muisc_add, this);
        this.addChild(this.music);
        /**音乐开关 */
        this.music_remove = new MButton(new MImage("shengyi_guanbi_png"), new MImage("shengyi_guanbi_png"));
        this.music_remove.x = 560; this.music_remove.y = 10;
        this.music_remove.addEventListener(egret.TouchEvent.TOUCH_TAP, this.muisc_remove, this);
        // this.addChild(this.music_remove);
        /**游戏玩法 按钮 */
        this.playing = new MButton(new MImage("wanfa_png"), new MImage("wanfa_png"));
        this.playing.x = 560; this.playing.y = 90;
        this.playing.addEventListener(egret.TouchEvent.TOUCH_TAP, this.addplaying, this);
        this.addChild(this.playing);
    }


    /**玩法 */
    private addplaying() {
        /**打开玩法界面  所有按钮不接受触摸事件 */
        this.btn.touchEnabled = false;//开始按钮
        this.music.touchEnabled = false;//音乐关闭
        this.music_remove.touchEnabled = false;//音乐开关
        this.playing.touchEnabled = false;//游戏玩法
        this.playingimage = new MImage("wenzi_jieshao_png");
        this.playingimage.x = 90; this.playingimage.y = 400;
        this.addChild(this.playingimage);
        this.close = new MButton(new MImage("jieshao_ganbi_png"), new MImage("jieshao_ganbi_png"));
        this.close.x = 460; this.close.y = 420;
        this.close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.addclose, this);
        this.addChild(this.close);

    }
    /**关闭玩法界面 */
    private addclose() {
        /**关闭玩法界面 所有按钮接受触摸事件 */
        this.btn.touchEnabled = true;//开始按钮
        this.music.touchEnabled = true;//音乐关闭
        this.music_remove.touchEnabled = true;//音乐开关
        this.playing.touchEnabled = true;//游戏玩法
        this.removeChild(this.playingimage);
        this.removeChild(this.close);
    }

    /**音乐的关闭 */
    private muisc_add(): void {
        SoundControl.getIns().stop(MUSIC_BG);
        SoundControl.getIns().stop(MUSIC_FIRE);
        // SoundControl.getIns().isNoVolume = 0;
        // SoundControl.getIns().setIsNoVolume(0);//音效静音
        this.removeChild(this.music);
        this.addChild(this.music_remove);
    }
    /**音乐的开关 */
    private muisc_remove(): void {
        SoundControl.getIns().play(MUSIC_BG);//音效
        // SoundControl.getIns().play(MUSIC_FIRE);//特效音效
        // SoundControl.getIns().isNoVolume = 1;
        // SoundControl.getIns().setIsNoVolume(1);//播放音效
        //SoundControl.getIns().play(MUSIC_BG, 0, 9999);//背景音乐
        this.removeChild(this.music_remove);
        this.addChild(this.music);
    }

    /**获取微信数据 */
    private async GetMessage() {
        await platform.login();
        await platform.showShareMenu();
        var userinfo = await platform.getUserInfo();//获取玩家信息
        console.log(userinfo);
        //	this.l.text = userinfo.nickName;
        this.nickName = userinfo.nickName;//将文本信息改为玩家昵称
        this.updateScore();
        if (userinfo.avatarUrl != null) {
            RES.getResByUrl(userinfo.avatarUrl, this.getResComplete, this, "image");//将玩家头像信息添加到预加载中
        }
        else {
            return;
        }
    }
    private getResComplete(date: any): void {//打印玩家头像，
        var head: egret.Bitmap = new egret.Bitmap(date);
        head.x = 10;
        head.y = 10;
        head.width = 100;
        head.height = 100;
        this.addChild(head);
    }
    /**昵称 */
    protected updateScore(): void {
        this.myScore = <number>this.nickName;
        this.txtMyScore.text = "昵称:" + this.myScore;
    }
}